View Javadoc
1   /*
2    * GenericNodeXmlResource.java
3    *
4    * Created on December 21, 2004, 1:08 PM
5    */
6   
7   package gov.noaa.eds.xapi.generic;
8   
9   import org.w3c.dom.Node;
10  import org.xml.sax.ContentHandler;
11  import org.xml.sax.SAXNotRecognizedException;
12  import org.xml.sax.SAXNotSupportedException;
13  import org.xmldb.api.base.ErrorCodes;
14  import org.xmldb.api.base.XMLDBException;
15  import org.xmldb.api.modules.XMLResource;
16  
17  /***
18   * A generic implementation of an XMLResource in which the only copy of the
19   * resource is in memory.
20   * @version $Id: GenericXmlNodeResource.java,v 1.2 2004/12/23 23:39:01 mrxtravis Exp $
21   * @author tns
22   */
23  public class GenericXmlNodeResource extends GenericResource implements XMLResource {
24      
25      private Node root = null;
26      private Object rootMutex = new Object();
27      
28      /*** Creates a new instance of GenericNodeXmlResource
29       *@param root The node root this resource wraps.
30       */
31      public GenericXmlNodeResource(Node root) {
32          if (root == null){
33              throw new NullPointerException("Parameter root can not be null");
34          }
35          this.root = root;
36      }
37      
38      /*** Throws UnsupportedOperationException 
39       *@todo implement this method
40       */
41      public boolean getSAXFeature(String str) throws org.xml.sax.SAXNotRecognizedException, org.xml.sax.SAXNotSupportedException {
42          throw new UnsupportedOperationException();
43      }
44      
45      /*** Returns 'XMLResource'
46       *@return 'XMLResource
47       */
48      public String getResourceType(){
49          return "XMLResource";
50      }
51      
52      /***Sets this resource to be the specified node 
53       *@param node The replacement node
54       */
55      public void setContentAsDOM(Node node) throws org.xmldb.api.base.XMLDBException {
56          Node parent = node.getParentNode();
57          synchronized (rootMutex){
58              if (parent != null) {
59                  parent.replaceChild(this.root,node);
60              }
61              this.root = node;
62          }
63      }
64      
65      /***Throws an XMLDBException with ErrorCodes.NOT_IMPLEMENTED.
66       *@todo implement this method
67       */
68      public void getContentAsSAX(org.xml.sax.ContentHandler contentHandler) throws XMLDBException {
69          throw new XMLDBException(ErrorCodes.NOT_IMPLEMENTED);
70      }
71      
72      /***Throws Unsupported Exception
73       *@todo implement this method
74       */
75      public void setSAXFeature(String str, boolean param) throws SAXNotRecognizedException, SAXNotSupportedException {
76          throw new UnsupportedOperationException();
77      }
78      
79      /*** Throws unsupppored exception
80       *@todo implement this method
81       */
82      public ContentHandler setContentAsSAX() throws XMLDBException {
83          throw new XMLDBException(ErrorCodes.NOT_IMPLEMENTED);
84      }
85      
86      /***Returns the ID of this resource
87       *@return the id
88       */
89      public String getDocumentId() throws org.xmldb.api.base.XMLDBException {
90          return this.getId();
91      }
92      
93      /***Returns the contents of this resource as a DOM node
94       *@return the node representation of this resource
95       */
96      public Node getContentAsDOM() {
97          return this.root;
98      }
99      
100     /***Sets the content if the obj type is a Node */
101     public void setContent(Object obj) throws XMLDBException {
102         if (obj instanceof Node){
103             this.setContentAsDOM( (Node) obj);
104         } else {
105             throw new XMLDBException(ErrorCodes.WRONG_CONTENT_TYPE);
106         }
107     }
108     
109     /***Returns the Node object */
110     public Object getContent(){
111         return this.getContentAsDOM();
112     }
113     
114 }